package com.idak.jaxb.library;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXB;

public class XmlJaxbLibraryImpl implements XmlJaxbLibrary {

	private Library libraryRepo;
	private Map<String, Book> books = new HashMap<String, Book>(0);
	private Map<String, Dvd>  dvds = new HashMap<String, Dvd>(0);
	
	public XmlJaxbLibraryImpl( InputStream inputStream) {
		libraryRepo = JAXB.unmarshal(inputStream, Library.class);
		
		for (Book book : libraryRepo.getBooks()) {
			books.put(book.getIsbn(), book);
		}
		
		for(Dvd dvd : libraryRepo.getDvds()){
			dvds.put(dvd.getId(), dvd);
		}
		
	}

	@Override
	public Book getBookByIsbn(String isbn) {
		return books.get(isbn);
	}

	@Override
	public Dvd getDvdById(String dvdId) {
		return dvds.get(dvdId);
	}

}
